package com;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import com.cdyne.ws.EmailVerificationClient;

public class NewGmailUser {
        private String firstName;
        private String userId;
        
		private String password;
		private String reEnterPassword;
        
		private String hitQuestion;
        private String hintAnswer;
        
        
        public String createNewUser(){
                try{
                        
                        if(userId!=null && userId.trim().equals("")){
                            FacesMessage message = new FacesMessage("Email Id is required....");
                            FacesContext.getCurrentInstance().addMessage("Email Id is required.....", message);
                            return "newuser";
                        }else if(!EmailVerificationClient.validateEmailId(userId)){
                        	System.out.println("NewGmailUser:createNewUser:validateEmailId false then true");
                            FacesMessage message = new FacesMessage("Given email id does not exist  ... pls give existing emailid....");
                            FacesContext.getCurrentInstance().addMessage("Given email id Does not exist  ... pls give existing emailid ....", message);
                            return "newuser";
                        }else if(password!=null && password.trim().equals("")){
                            FacesMessage message = new FacesMessage("Password is required");
                            FacesContext.getCurrentInstance().addMessage("Password is required", message);
                            return "newuser";
                        }else if(reEnterPassword!=null && reEnterPassword.trim().equals("")){
                            FacesMessage message = new FacesMessage("reEnterPassword is required");
                            FacesContext.getCurrentInstance().addMessage("reEnterPassword is required", message);
                            return "newuser";
                        }else if(!reEnterPassword.trim().equals(password.trim())){
                            FacesMessage message = new FacesMessage("both passwords should be same");
                            FacesContext.getCurrentInstance().addMessage("both passwords should be same", message);
                            return "newuser";
                        }else if(hitQuestion!=null && hitQuestion.trim().equals("")){
                        	FacesMessage message = new FacesMessage("Hint Question is required");
                        	FacesContext.getCurrentInstance().addMessage("Hint Question is required", message);
                        	return "newuser";
                        }else if(hintAnswer!=null && hintAnswer.trim().equals("")){
                            FacesMessage message = new FacesMessage("hintAnswer is required");
                            FacesContext.getCurrentInstance().addMessage("hintAnswer is required", message);
                            return "newuser";
                        }
                        System.out.println("firstName:"+firstName);
                        System.out.println("userId:"+userId);
                        System.out.println("password:"+password);
                        System.out.println("hitQuestion:"+hitQuestion);
                        System.out.println("hintAnswer:"+hintAnswer);
                        
                        Properties userProps = new Properties();
                        userProps.load(new FileInputStream(getSelectedContextPath()+"/SourceFolder/UserDetails.properties"));
                        System.out.println("belore**************"+userId);
                        if(userProps.containsKey(userId)){
                                System.out.println("User already exist userId:"+userId);
                                FacesMessage message = new FacesMessage("User already exist");
                                FacesContext.getCurrentInstance().addMessage("User already exist", message);
                                return "newuser";
                        }
                        userProps.put(userId, password);
                        userProps.store(new FileOutputStream(getSelectedContextPath()+"/SourceFolder/UserDetails.properties"),"Saving UserDetails");
                        
                        Properties forgetProps = new Properties();
                        forgetProps.load(new FileInputStream(getSelectedContextPath()+"/SourceFolder/forGetPassword.properties"));
                        
                        forgetProps.put(userId, hitQuestion+"?"+hintAnswer);
                        forgetProps.store(new FileOutputStream(getSelectedContextPath()+"/SourceFolder/forGetPassword.properties"),"Saving forgetPassword");
                        
//                        FacesMessage message = new FacesMessage("User Registered Successfully Click GoToLoginPage to Enter to IDE");
//                        FacesContext.getCurrentInstance().addMessage("User Registered Successfully Click GoToLoginPage to Enter to IDE", message);
                        }catch(Exception e){
                                e.printStackTrace();
                        }
                                
                return "confirm";
        }
        public static String getSelectedContextPath(){
                String contextPath = null;
                FacesContext context = FacesContext.getCurrentInstance();
                ServletContext servletContext = (ServletContext)context.getExternalContext().getContext();
                contextPath = servletContext.getRealPath("");
                contextPath = contextPath.substring(0, contextPath.lastIndexOf(IDEConstants.FOLDER_ON_SERVER+File.separator));
                System.out.println("New Gmail User Servlet context path ::::"+contextPath);
                return contextPath;
        }
        
        public String goToLoginPage(){
                return "login";
        }
        public String getHitQuestion() {
                        return hitQuestion;
                }
                public void setHitQuestion(String hitQuestion) {
                        this.hitQuestion = hitQuestion;
                }
                public String getHintAnswer() {
                        return hintAnswer;
                }
                public void setHintAnswer(String hintAnswer) {
                        this.hintAnswer = hintAnswer;
                }
        public String getFirstName() {
                return firstName;
        }
        public void setFirstName(String firstName) {
                this.firstName = firstName;
        }
        
        public String getPassword() {
                return password;
        }
        public void setPassword(String password) {
                this.password = password;
        }
        public String getUserId() {
			return userId;
		}
		public void setUserId(String userId) {
			this.userId = userId;
		}
		public String getReEnterPassword() {
			return reEnterPassword;
		}
		public void setReEnterPassword(String reEnterPassword) {
			this.reEnterPassword = reEnterPassword;
		}
}